<template>
  <div>
    <el-container>
      <el-header class="homeHeader">
        <div class="homeHeader-title">管理系统</div>
          <div>
            <el-button icon="el-icon-bell" @click="goChat" type="primary" style="margin-right: 5px;padding: 0px"></el-button>
            <el-dropdown  @command="commandHandler" class="userinfo" >
        <span class="el-dropdown-link" >
          {{user.name}}<img :src="user.userface" /><i class="el-icon-arrow-down el-icon--right"></i>
        </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="userinfo">个人中心</el-dropdown-item>
                <el-dropdown-item command="setting">设置</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
      </el-header>

      <el-container >
        <el-aside width="200px">
          <el-menu router unique-opened>
            <el-submenu :index="index+''" v-for="(route,index) in routes" v-if="!route.hidden" :key="index">
              <template slot="title">
                <i style="color:#303133" :class="route.iconCls"></i>
                <span>{{route.name}}</span>
              </template>
                <el-menu-item :index="child.path" v-for="(child,index) in route.children" :key="index">{{child.name}}</el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <el-main>
          <el-breadcrumb separator-class="el-icon-arrow-right" v-if="this.$router.currentRoute.path!='/home'">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
          </el-breadcrumb>
          <div class="homeWelcome" v-if="this.$router.currentRoute.path=='/home'">
            欢迎
          </div>
          <router-view class="HOME-ROUTE"/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "Home",
  data(){
    return{
      user:JSON.parse(window.sessionStorage.getItem("user")),
    }
  },
  computed:{
    routes(){
      return this.$store.state.routes;
    }
  },
  mounted() {
    this.$store.dispatch("connect");
  },
  methods:{
    menuClick(index){
      this.$router.push(index);
    },
    goChat(){
      // this.$router.push('/chat');
      let chat = this.$router.resolve({
        path:'/chat'
      })
      window.open(chat.href,'_blank')
    },
    commandHandler(cmd){
      if(cmd=='logout'){
        this.$confirm('确定注销？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.postRequest('/logout');
          window.sessionStorage.removeItem("user");
          this.$store.commit("initRoutes",[]);
          this.$router.replace("/")
          this.$message.success("注销成功")

        }).catch(() => {

        });
      }
    }
  }
}
</script>

<style>
.homeHeader{
  background-color: #409EFF;
  display: flex;
  text-align: center;
  justify-content: space-between;
  padding: 0px 15px;
  box-sizing: border-box;
  line-height: 60px;
}
.homeHeader-title{
  font-size: 30px;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  color: snow;
}
.userinfo{
  font-size: 15px;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  color: snow;
  cursor: pointer;
}
.el-dropdown-link img{
  width: 48px;
  height: 48px;
  border-radius: 24px;
  margin-left: 8px;
}
.el-dropdown-link{
  display: flex;
  align-items: center;
}
.homeWelcome{
  text-align: center;
  font-size: 30px;
  color: #409EFF;
}
.HOME-ROUTE{
  margin-top: 10px ;
}

</style>